package com.my.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

/**
 * @author mayong
 * @since 2024/4/7
 */
@UtilityClass
@Slf4j
public class JsonUtil {

    ObjectMapper objectMapper = new ObjectMapper();

    public String json(Object val) {
        try {
            return objectMapper.writeValueAsString(val);
        } catch (JsonProcessingException e) {
            log.error("序列化json异常");
        }
        return null;
    }


    public  <T> T object(String s, Class<T> clazz) {
        try {
            return objectMapper.readValue(s, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("反序列化json发生异常");
        }

    }

    public <T> T convertValue(Object obj, Class<T> clazz){
        return objectMapper.convertValue(obj, clazz);
    }
}
